<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Admin\Controller;

/**
 * Description of UsersController
 *
 * @author zhouyehao
 */
class UsersController extends AdminController {

    public function index() {

        $where .= '1=1 ';

        $keywords = I('keywords');
        if ($keywords) {
            $where .= " and ( u.nick_name like '%" . $keywords . "%' or u.phone = '" . $keywords . "')";
            $this->assign('keywords', $keywords);
        }
        //用户状态
        $status = I("status", -1);
        if ($status != -1) {
            $where .= ' and u.status = ' . $status;
//            $where = array('EQ',$status);
            $this->assign("status", $status);
        }
        //日期选择
        $start_date = I("start_date");
        $end_date = I("end_date");


        if (!empty($end_date) && !empty($start_date)) {
            if ($end_date <= $start_date) {
                $this->error('开始时间不允许小于等于结束时间');
                exit;
            } else {
                $where .= " and u.create_time between '$start_date 00:00:00' AND '$end_date 23:59:59'";
//                $where['sp.create_time'] = array("between", array($start_date, $end_date . ' 23:59:59'));
            }
            $this->assign("start_date", $start_date);
            $this->assign("end_date", $end_date);
        } else {
            if (!empty($start_date)) {
//                $where['sp.create_time'] = array('EGT', $start_date);
                $where .= " and u.create_time >='$start_date 00:00:00'";
                $this->assign("start_date", $start_date);
            }

            if (!empty($end_date)) {
//                $where['sp.create_time'] = array('ELT', $end_date . ' 23:59:59');
                $where .= " and u.create_time <= '$end_date 23:59:59'";
                $this->assign("end_date", $end_date);
            }
        }

        $M = M()->table("ot_users u");

        $field = 'u.*';

        $order = "u.user_id desc";
        $list = $this->lists($M, $where, $order, $field);

        $this->assign('_list', $list);
        $this->assign('meta_title', '用户管理');
        $this->display();
    }

    public function changeStatus() {
        $id = I("id", 0, 'intval');
        $status = I("s", 0, 'intval');

        if (!$id) {
            $this->error('参数错误');
        }

//        $data['user_id'] = $id;
        $data['status'] = $status;
        $data['update_time'] = date('Y-m-d H:i:s', time());
        $res = M("Users")->where(['user_id'=>$id])->save($data);
        if ($res) {
            $this->success('操作成功', U('index'));
        } else {
            $this->error('操作失败');
        }
    }


}
